var piMessage = new i18nMessage('../../servlet/I18nMessageServlet?keyPrefix=ui.productidentification');
var referenceMessage = new i18nMessage('../../servlet/I18nMessageServlet?keyPrefix=ui.reference');

var ctSeasonNumberXmlLoader = new i18nMessage('../../servlet/CodeTableServlet?name=CtSeasonNumber');
var ctVersionXmlLoader = new i18nMessage('../../servlet/CodeTableServlet?name=CtVersion');
var ctProductFamilyXmlLoader = new i18nMessage('../../servlet/CodeTableServlet?name=CtProductFamily');
var ctDesignTypeXmlLoader = new i18nMessage('../../servlet/CodeTableServlet?name=CtDesignType');
			
var orderItemStore;
var orderItemGrid;
var orderItemWindow;

var selectOrderItem = function(){
    if (!orderItemWindow) {
        orderItemWindow = new Ext.Window({
            title: message.get('message.combo.pleaseselect'),
            layout: 'fit',
            width: 920,
            height: 680,
            closeAction: 'hide',
            plain: true,
            modal: true,
            items: [{
                id: 'orderItemPanel',
                region: 'center',
                bodyStyle: {
                    background: '#ffffff',
                    padding: '7px'
                },
                html: '<div id="orderItemDiv"></div>'
            }]
        });
    }
    orderItemWindow.show();
    
    if (orderItemGrid) {
		orderItemStore.reload();
		return;
	}
    orderItemStore = new Ext.data.Store({
        url: '../../servlet/OrderServlet?action=4',
        
        reader: new Ext.data.XmlReader({
            record: 'item',
            id: 'id',
            totalRecords: 'itemTotal'
        }, ['index', 'id', 'pantoneNumber', 'colorNumber', 'realColor', {
            name: 'amountS',
            type: 'int'
        }, {
            name: 'amountM',
            type: 'int'
        }, {
            name: 'amountL',
            type: 'int'
        }, {
            name: 'amountXl',
            type: 'int'
        }, {
            name: 'amountXxl',
            type: 'int'
        }, {
            name: 'amountXxxl',
            type: 'int'
        }, {
            name: 'total',
            type: 'int'
        }, 'colorA', 'pantoneNumberA', 'realColorA', 'percentageA', 'colorB', 'pantoneNumberB', 'realColorB', 'percentageB', 'colorC', 'pantoneNumberC', 'realColorC', 'percentageC', 'colorD', 'pantoneNumberD', 'realColorD', 'percentageD', 'colorE', 'pantoneNumberE', 'realColorE', 'percentageE', 'colorF', 'pantoneNumberF', 'realColorF', 'percentageF', 'colorG', 'pantoneNumberG', 'realColorG', 'percentageG', 'colorH', 'pantoneNumberH', 'realColorH', 'percentageH',
		{
		 	name: 'productIdentification.ctProductFamily',
		 	mapping: 'ctProductFamily'
		 },
		'ctMachineType', 'type', 'orderNumber',
		 'yarnNumber', 'dhlNumber', 
		 'style', 'customerLinkman', 'model', {
		 	name: 'productIdentification.ctSeasonNumber',
		 	mapping: 'ctSeasonNumber'
		 }, {
		 	name: 'productIdentification.reference',
		 	mapping: 'reference'
		 },{
		 	name: 'productIdentification.orderNumber',
		 	mapping: 'orderNumber'
		 },{
		 	name: 'productIdentification.ctVersion',
		 	mapping: 'ctVersion'
		 },{
		 	name: 'productIdentification.patronageNumer',
			mapping: 'patronageNumer'
		 },{
		 	name: 'productIdentification.ctDesignType',
			mapping: 'ctDesignType'
		 }]),
        
        remoteSort: true
    });
    
    orderItemStore.setDefaultSort('id', 'desc');
    
    function renderSelectItem(value, p, record){
        return String.format('<a href="javascript: selectOrderItemCallback(\'{0}\', \'{1}\', \'{2}\', \'{3}\');orderItemWindow.hide();"><img src="../../images/icons/arrow_rotate_clockwise.png" title="' + message.get('message.list.select') + '" /></a>', record.id,record.data['productIdentification.orderNumber'], record.data['productIdentification.reference'], record.data.colorNumber);
    }
	
	var piFilters = new Ext.grid.GridFilters({
		  filters:[
		  	{type: 'list',  dataIndex: 'productIdentification.ctSeasonNumber', options: ctSeasonNumberXmlLoader.getCtArray() },
			{type: 'list',  dataIndex: 'productIdentification.ctProductFamily', options: ctProductFamilyXmlLoader.getCtArray() },
			{type: 'list',  dataIndex: 'productIdentification.ctDesignType', options: ctDesignTypeXmlLoader.getCtArray() },
			{type: 'list',  dataIndex: 'productIdentification.ctVersion', options: ctVersionXmlLoader.getCtArray() },
		    {type: 'string',  dataIndex: 'productIdentification.reference'},
			{type: 'string',  dataIndex: 'productIdentification.patronageNumer'},
			{type: 'string',  dataIndex: 'productIdentification.orderNumber'},
			{type: 'string',  dataIndex: 'colorNumber'}
	]});
    
    orderItemGrid = new Ext.grid.GridPanel({
        title: orderMessage.get('ui.order.listtitle'),
        store: orderItemStore,
        stripeRows: true,
        height: 630,
        columns: [{
            header: message.get('message.list.id'),
            width: 50,
            dataIndex: 'index',
            sortable: false
        },{
            header: message.get('message.edit.productfamily'),
            dataIndex: 'productIdentification.ctProductFamily',
            sortable: true
        },{
            header: piMessage.get('ui.productidentification.designtype'),
            dataIndex: 'productIdentification.ctDesignType',
            sortable: true
        },{
            header: piMessage.get('ui.productidentification.style'),
            dataIndex: 'style',
            sortable: true
        },{
            header: piMessage.get('ui.productidentification.seasonnumber'),
            dataIndex: 'productIdentification.ctSeasonNumber',
            sortable: true
        },{
            header: piMessage.get('ui.productidentification.patronagenumer'),
            dataIndex: 'productIdentification.patronageNumer',
            sortable: true
        },{
            header: piMessage.get('ui.productidentification.chartversion'),
            dataIndex: 'productIdentification.ctVersion',
            sortable: true
        },{
            header: piMessage.get('ui.productidentification.ordernumber'),
            dataIndex: 'productIdentification.orderNumber',
            sortable: true
        },{
            header: referenceMessage.get('ui.reference.reference'),
            dataIndex: 'productIdentification.reference',
            sortable: true
        },{
            id: 'colorNumber',
            header: orderMessage.get('ui.order.color'),
			sortable: true,
            dataIndex: 'colorNumber'
        }, {
            header: 'S',
            dataIndex: 'amountS',
            sortable: true,
            align: 'right',
			
            summaryType: 'sum'
        }, {
            header: 'M',
            dataIndex: 'amountM',
            sortable: true,
            align: 'right'
        }, {
            header: 'L',
            dataIndex: 'amountL',
            sortable: true,
            align: 'right'
        }, {
            header: 'XL',
            dataIndex: 'amountXl',
            sortable: true,
            align: 'right'
        }, {
            header: 'XXL',
            dataIndex: 'amountXxl',
            sortable: true,
            align: 'right'
        }, {
            header: 'XXXL',
            align: 'right',
            dataIndex: 'amountXxxl',
            sortable: true
        }, {
            header: message.get('message.list.total'),
            id: 'total',
            dataIndex: 'total',
            align: 'right',
            sortable: true
        }, {
            header: message.get('message.list.select'),
            width: 50,
            dataIndex: 'id',
            sortable: true,
            renderer: renderSelectItem
        }],
        viewConfig: {
            forceFit: true
        },
        loadMask: true,
        plugins: [piFilters],
        renderTo: 'orderItemDiv'
    });
    
    orderItemStore.load();
    
    var startPage = getPrarmeterValue(window.location, 'startPage');
    var start;
    if (startPage == null || startPage == '') {
        start = 0;
    }
    else {
        start = (startPage - 1) * 20;
    }
    orderItemStore.load({
        params: {
            start: start,
            limit: 20
        }
    });
}

